package com.mouse.dnf.service;

import com.mouse.damo.micro.*;
import com.mouse.damo.service.CustomService;
import com.mouse.damo.service.Point;
import com.mouse.damo.util.StringUtil;
import com.mouse.dnf.HuoSanApplication;
import com.mouse.dnf.enums.MapHuoSan;

import java.io.File;
import java.util.Arrays;
import java.util.List;
import java.util.Random;
import java.util.stream.Stream;

public class HuoSanService {
    //TODO
    private static String appName = "DNF Taiwan";

    private static Com grobalCom = new Com();
    private static Window window = new Window(grobalCom);
    private CustomService customService = new CustomService();
    private boolean start = false;

    public void setStart(boolean start) {
        this.start = start;
    }

    public boolean checkHwnd(Integer hwnd) {
        Window window = new Window(grobalCom);
        return window.windowIsExist(hwnd);
    }

    public void auto(Com com) {
        while (true) {
            Sleep.sleep(3000);
            if (!start) continue;
            try {
                HuoSanApplication.setLog("城中");
                Window window = new Window(com);
                int[] clientSize = window.getClientSize(com.getHwnd());
                //检查疲劳
                checkPiLao(com, clientSize);
                boolean wangXiaZou = wangXiaZou(com, clientSize);
                if (!wangXiaZou) continue;
                boolean jinTuFlag = xuanTu(com, clientSize);
                if (!jinTuFlag) continue;
                boolean guoTu = guoTu(com, clientSize);
                while (guoTu) {
                    Sleep.sleep(5000);
                    if (!start) continue;
                    guoTu = guoTu(com, clientSize);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    private boolean guoTu(Com com, int[] clientSize) {
        FindPic findPic = new FindPic(com);
        Mouse mouse = new Mouse(com);
        Press press = new Press(grobalCom);
        for (MapHuoSan mapEnum : MapHuoSan.values()) {
            Point tuPoint = customService.loopPoint(20, () -> new Point(findPic.findPic(clientSize[1] - 300, 0,
                    clientSize[1], 300, mapEnum.getBmp(), "203040", 0.8, 0)));
            if (!tuPoint.isSuccess()) {
//                huiCheng(com, clientSize);
                return false;
            }
            press.keyPress(Press.X);
            Sleep.sleep(500);
            press.keyPress(Press.X);
            Sleep.sleep(500);
            press.keyPress(Press.X);
            Sleep.sleep(500);
            press.keyPress(Press.X);
            Sleep.sleep(2500 );
//            window.setWindowActivate(com.getHwnd());
            press.keyPressTime(mapEnum.getPressCode(), 200);
            Sleep.sleep(500);
            press.groupPress(Press.ALT, mapEnum.getPressCode());
            Sleep.sleep(200);
            press.groupPress(Press.ALT, mapEnum.getPressCode());
            Sleep.sleep(200);
            press.groupPress(Press.ALT, mapEnum.getPressCode());
            Sleep.sleep(200);
            press.groupPress(Press.ALT, mapEnum.getPressCode());
            Sleep.sleep(200);
        }

        //翻牌子
        fanPaiZi(mouse, findPic, clientSize);
        //继续
        Point tuPoint = customService.loopPoint(10, () -> new Point(findPic.findPic(0, 0,
                clientSize[1], clientSize[2], "继续.bmp", "203040", 0.8, 0)));
        if (tuPoint.isSuccess()) {
            mouse.mouseClick(tuPoint.getX(), tuPoint.getY() + 40, true);
            Sleep.sleep(200);
            mouse.mouseDoubleClick(tuPoint.getX(), tuPoint.getY() + 40);
            Sleep.sleep(200);
            mouse.mouseClick(tuPoint.getX(), tuPoint.getY() + 40, true);
            Sleep.sleep(200);
            mouse.mouseClick(tuPoint.getX(), tuPoint.getY() + 40, true);
            Sleep.sleep(5 * 1000);
            return true;
        }
        return false;
    }

    private void fanPaiZi(Mouse mouse, FindPic findPic, int[] clientSize) {
        Point tuPoint = customService.loopPoint(30, () -> new Point(findPic.findPic(0, 0,
                clientSize[1], clientSize[2], "奖励.bmp", "203040", 0.8, 0)));
        if (!tuPoint.isSuccess()) return;
        Sleep.sleep(4000);
        //翻牌子
        HuoSanApplication.setLog("翻牌子");
        List<Integer> paiList = Arrays.asList(220, 420, 620, 820);//牌子
        Integer paiX = paiList.get(new Random().nextInt(4));
        mouse.mouseClick(paiX, 250, true);
        Sleep.sleep(100);
        mouse.mouseClick(paiX, 250, true);
        Sleep.sleep(100);
        mouse.mouseClick(paiX, 250, true);
        Sleep.sleep(100);
        mouse.mouseClick(paiX, 250, true);
    }

    private boolean xuanTu(Com com, int[] clientSize) {
        Mouse mouse = new Mouse(com);
        FindPic findPic = new FindPic(com);
        int width = clientSize[1], height = clientSize[2];

        Point tuPoint = customService.loopPoint(5, () -> new Point(findPic.findPic(0, 0, width, height,
                "火山1.bmp|火山2.bmp", "203040", 0.8, 0)));
        if (!tuPoint.isSuccess()) return false;
        window.setWindowActivate(com.getHwnd());
        Sleep.sleep(500);
        mouse.mouseClick(tuPoint.getX(), tuPoint.getY(), true);
        Sleep.sleep(300);
        mouse.mouseDoubleClick(tuPoint.getX(), tuPoint.getY());
        Sleep.sleep(300);
        mouse.mouseDoubleClick(tuPoint.getX(), tuPoint.getY());
        Sleep.sleep(300);
        mouse.mouseClick(tuPoint.getX(), tuPoint.getY(), true);
        //判断是否进图 TODO
        HuoSanApplication.setLog("判断是否在图里");
        boolean checkFlag = checkInTu(com, width, height);
        if (!checkFlag) return false;
        return true;
    }

    private boolean checkInTu(Com com, int width, int height) {
        FindPic findPic = new FindPic(com);
        Point inTuPoint = customService.loopPoint(20, () -> new Point(findPic.findPic(width - 300, 0, width, 300,
                "在图.bmp", "203040", 0.8, 0)));
        if (!inTuPoint.isSuccess()) return false;
        return true;
    }

    private boolean wangXiaZou(Com com, int[] clientSize) {
        int width = clientSize[1], height = clientSize[2];
        FindPic findPic = new FindPic(com);
        Point piLaoPoint = customService.loopPoint(2, () -> new Point(findPic.findPic(0, 0, width, height,
                "城中.bmp", "203040", 0.8, 0)));
        if (!piLaoPoint.isSuccess()) {
            HuoSanApplication.setLog("不在城中");
            return false;
        }
        window.setWindowActivate(com.getHwnd());
        Sleep.sleep(500);
        Press press = new Press(grobalCom);
        press.keyPressTime(Press.DOWN, 3000);
        return true;
    }

    private void checkPiLao(Com com, int[] clientSize) {
        //检查疲劳
        FindPic findPic = new FindPic(com);
        Mouse mouse = new Mouse(com);
        //空疲劳
        HuoSanApplication.setLog("检查疲劳");
        Point piLaoPoint = customService.loopPoint(5, () -> new Point(findPic.findPic(0, 0, clientSize[1], clientSize[2],
                "没疲劳.bmp", "203040", 0.8, 0)));
        if (piLaoPoint.isSuccess()) {
            //点击数字6
            window.setWindowActivate(com.getHwnd());
            Sleep.sleep(500);
            mouse.mouseClick(piLaoPoint.getX() - 47, piLaoPoint.getY() + 27, false);
            Sleep.sleep(100);
            mouse.mouseClick(piLaoPoint.getX() - 47, piLaoPoint.getY() + 27, false);
            Sleep.sleep(100);
            mouse.mouseClick(piLaoPoint.getX() - 47, piLaoPoint.getY() + 27, false);
            Sleep.sleep(100);
            mouse.mouseClick(piLaoPoint.getX() - 47, piLaoPoint.getY() + 27, false);
            Sleep.sleep(500);
        }
    }

    public Com findHwnd() {
        String windows = window.findWindows(appName);
        if (StringUtil.isBlank(windows)) return null;
        String hwndStr = Stream.of(windows.split(",")).filter(s -> window.getWindowClass(Integer.valueOf(s))
                .equalsIgnoreCase(appName)).findFirst().orElse(null);
        if (StringUtil.isBlank(windows)) return null;
        Com bingCom = new Com();
//        File dnfResource = new File("dnfResource");
        File dnfResource = new File("D:\\dnf");
        bingCom.setResourcePath(dnfResource.getAbsolutePath());
        boolean bind = bingCom.bind(Integer.valueOf(hwndStr));
        HuoSanApplication.setLog("绑定结果:" + bind);
        return bingCom;
    }

    //回城
    private boolean huiCheng(Com com, int[] clientSize) {
        Press press = new Press(grobalCom);
        Mouse mouse = new Mouse(com);
        int width = clientSize[1], height = clientSize[2];
        FindPic findPic = new FindPic(com);
        for (int i = 0; i < 3; i++) {
            window.setWindowActivate(com.getHwnd());
            Sleep.sleep(400);
            press.keyPress(Press.ESC);
            //TODO
            Point youPoint = customService.loopPoint(10, () -> new Point(findPic.findPic(0, 0, width, height,
                    "回城.bmp", "203040", 0.8, 0)));
            if (youPoint.isSuccess()) {
                mouse.mouseClick(youPoint.getX(), youPoint.getY(), true);
                return true;
            }
        }
        HuoSanApplication.setLog("回城失败");
        return false;
    }
}
